package uutalk.netcloud.talk.app.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.base.util.MediaManager;
import uutalk.netcloud.talk.base.util.thirdpart.ImageLoader;
import uutalk.netcloud.talk.base.util.thirdpart.QiNiuManager;
import uutalk.netcloud.talk.base.util.thirdpart.UserManager;
import uutalk.netcloud.talk.model.bean.DBEntityManager;
import uutalk.netcloud.talk.model.bean.GroupHistoryInfo;
import uutalk.netcloud.talk.model.bean.GroupMember;
import uutalk.netcloud.talk.model.bean.UserInfo;

import static com.baidu.mapapi.BMapManager.getContext;

/**
 * Created by Administrator on 2017/12/14.
 */

public class GroupHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    final String TAG = "GroupHistoryAdapter";
    private Context context;
    List<GroupHistoryInfo> groupHistoryInfoList;
    private Handler mHandler;
    private final int TYPE_SEND = 0;
    private final int TYPE_REC = 1;
    private List<Integer> Livelist=new ArrayList<>();

    private int mPosition;


    Bitmap mDefaultBitmap = null;

    UserInfo userInfo = UserManager.getInstance().getUserInfo();

    public GroupHistoryAdapter(Context context, List<GroupHistoryInfo> groupHistoryInfoList) {
        if (groupHistoryInfoList == null) {
            this.groupHistoryInfoList = new ArrayList<>();
        } else {
            this.groupHistoryInfoList = groupHistoryInfoList;
        }
        this.context = context;
        mDefaultBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.default_head);
    }

    public GroupHistoryInfo getLastGroupHistoryInfo() {
        return groupHistoryInfoList.get(getItemCount() - 1);
    }

    public void addItems(List<GroupHistoryInfo> e) {
        groupHistoryInfoList.addAll(e);
        notifyDataSetChanged();
    }

    public void addItem(GroupHistoryInfo e) {
        groupHistoryInfoList.add(e);
        notifyDataSetChanged();
    }

    public void deleteAll() {
        groupHistoryInfoList.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_SEND){
            View view = LayoutInflater.from(context).inflate(R.layout.group_history_item_right, parent,false);
            return new GroupHistoryAdapter.SendViewHolderRight(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.group_history_item_left, parent,false);
            return new GroupHistoryAdapter.RecViewHolderleft(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final GroupHistoryInfo groupHistoryInfo = groupHistoryInfoList.get(position);
        final GroupMember groupMember = DBEntityManager.getGroupMember(groupHistoryInfo.getUser_id());
        if(holder instanceof SendViewHolderRight){
            Log.e("GroupHistoryAdapter","数据="+groupHistoryInfo.toString());
            SendViewHolderRight sendViewHolderRight = (SendViewHolderRight)holder;
            sendViewHolderRight.tvTime.setText(groupHistoryInfo.getTime());
            if(TextUtils.isEmpty(UserManager.getInstance().getUserInfo().getPicture())){
                sendViewHolderRight.myImg.setImageBitmap(mDefaultBitmap);
            } else {
                ImageLoader.loadImage(getContext(), QiNiuManager.QINIU_HOST + UserManager.getInstance().getUserInfo().getPicture(), sendViewHolderRight.myImg);
            }

            if(!TextUtils.isEmpty(groupHistoryInfo.getText())){
               sendViewHolderRight.myText.setText(groupHistoryInfo.getText());
            }else {
                sendViewHolderRight.myText.setVisibility(View.GONE);
            }

//            sendViewHolderRight.myAudio.setTag(position);
//            if (Livelist.contains(position)){
//                sendViewHolderRight.myView.setBackgroundResource(R.drawable.my_adj1);
//            }
//            else {
//
//            }
            sendViewHolderRight.myAudio.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    groupHistoryInfo.setAudio_item(position);
                    if(MediaManager.isPlaying()){
                        refresh(groupHistoryInfo.getAudio_item());
                    }
                    sendViewHolderRight.myView=(ImageView)view.findViewById(R.id.id_recorder_anim_right);
                    sendViewHolderRight.myView.setBackgroundResource(R.drawable.my_play);
                    AnimationDrawable drawable = (AnimationDrawable) sendViewHolderRight.myView
                            .getBackground();
                    drawable.start();
                    // 播放音频
                    MediaManager.playSound(groupHistoryInfo.getVoice_url(),
                            new MediaPlayer.OnCompletionListener() {

                                @Override
                                public void onCompletion(MediaPlayer mp) {
                                    sendViewHolderRight.myView.setBackgroundResource(R.drawable.my_adj1);
                                }
                            });
                }
            });
        }


        else if(holder instanceof RecViewHolderleft){
            RecViewHolderleft recViewHolderleft = (RecViewHolderleft)holder;
            Log.e(TAG, " userName"+groupHistoryInfo.getUser_name() + " getText>>"+groupHistoryInfo.getText());
            if(groupMember == null || TextUtils.isEmpty(groupMember.getUser_id()) || TextUtils.isEmpty(groupMember.getUser_picture())){
                recViewHolderleft.otherImg.setImageBitmap(mDefaultBitmap);
            } else {
                Log.e("GroupHistoryAdapter","URL="+groupMember.getUser_picture().toString());
                ImageLoader.loadImage(context, QiNiuManager.QINIU_HOST + groupMember.getUser_picture(), recViewHolderleft.otherImg);
            }

            recViewHolderleft.tvTime.setText(groupHistoryInfo.getTime());
            recViewHolderleft.otherName.setText(groupHistoryInfo.getUser_name());
            recViewHolderleft.otherText.setText(groupHistoryInfo.getText());
            if(!TextUtils.isEmpty(groupHistoryInfo.getText())){
                recViewHolderleft.otherText.setText(groupHistoryInfo.getText());
            }else {
                recViewHolderleft.otherText.setVisibility(View.GONE);
            }
            recViewHolderleft.otherAudio.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    groupHistoryInfo.setAudio_item(position);
                    if(MediaManager.isPlaying()){
                        refresh(groupHistoryInfo.getAudio_item());
                    }
                    recViewHolderleft.otherView.setBackgroundResource(R.drawable.other_play);
                    AnimationDrawable drawable = (AnimationDrawable) recViewHolderleft.otherView
                            .getBackground();
                    drawable.start();
                    // 播放音频
                    MediaManager.playSound(groupHistoryInfo.getVoice_url(),
                            new MediaPlayer.OnCompletionListener() {
                                @Override
                                public void onCompletion(MediaPlayer mp) {
                                    recViewHolderleft.otherView.setBackgroundResource(R.drawable.other_adj1);
                                }
                            });
                }
            });

        } else {

        }
    }

    @Override
    public int getItemCount() {
        return groupHistoryInfoList.size();
    }

    private void refresh(int item){
        notifyItemChanged(item);
    }


    public static class SendViewHolderRight extends RecyclerView.ViewHolder{

        TextView tvTime,  myText;
        RoundedImageView myImg;
        LinearLayout my_ll;
        FrameLayout myAudio;
        ImageView myView;

        public SendViewHolderRight(View itemView) {
            super(itemView);
            tvTime = (TextView) itemView.findViewById(R.id.item_list_tame);
            myText = (TextView) itemView.findViewById(R.id.item_right_text);
            myImg = (RoundedImageView) itemView.findViewById(R.id.item_Img_riget);
            my_ll = (LinearLayout) itemView.findViewById(R.id.list_ll_right);
            myAudio = (FrameLayout) itemView.findViewById(R.id.recorder_length_right);
            myView = (ImageView) itemView.findViewById(R.id.id_recorder_anim_right);
        }
    }

    public static class RecViewHolderleft extends RecyclerView.ViewHolder{

        TextView otherName, tvTime, otherText;
        RoundedImageView otherImg;
        RelativeLayout other_rl;
        FrameLayout otherAudio;
        View otherView;

        public RecViewHolderleft(View itemView) {
            super(itemView);
            otherName = (TextView) itemView.findViewById(R.id.item_user_name);
            tvTime = (TextView) itemView.findViewById(R.id.item_list_tame);
            otherText = (TextView) itemView.findViewById(R.id.item_left_text);
            otherImg = (RoundedImageView) itemView.findViewById(R.id.item_Img_left);
            other_rl = (RelativeLayout) itemView.findViewById(R.id.item_list_ll_left);
            otherAudio = (FrameLayout) itemView.findViewById(R.id.recorder_length_left);
            otherView = (View) itemView.findViewById(R.id.id_recorder_anim_left);
        }
    }

    @Override
    public int getItemViewType(int position) {
        final GroupHistoryInfo groupHistoryInfo = groupHistoryInfoList.get(position);
        if(groupHistoryInfo.getUser_id().equals(UserManager.getInstance().getUserInfo().getUser_id())){
            return TYPE_SEND;
        } else {
            return TYPE_REC;
        }
    }

    public void gc(){
        if(mDefaultBitmap != null){
            mDefaultBitmap.recycle();
            mDefaultBitmap = null;
        }
    }

}
